package com.glory.hikvisionmobile.activity;

import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.EqpEventSpinnerAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.EqpWork.EqpStateInfo;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.customView.HikLabelEditText;
import com.glory.hikvisionmobile.customView.HikLabelSpinner2;
import com.glory.hikvisionmobile.customView.HikLabelTextView;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;


import butterknife.BindView;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/3/16 15:16
 * @DESC *****************************
 */
public class Eqp_StateModifyActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lbEqpID)
    HikLabelEditText lbEqpID;
    @BindView(R.id.lbDesc)
    HikLabelTextView lbDesc;
    @BindView(R.id.lbState_category)
    HikLabelTextView lbState_category;
    @BindView(R.id.lbState)
    HikLabelTextView lbState;
    @BindView(R.id.lbState_sub)
    HikLabelTextView lbState_sub;
    @BindView(R.id.lbSpinner)
    HikLabelSpinner2 lbSpinner;
    @BindView(R.id.lbState_target)
    HikLabelTextView lbState_target;
    @BindView(R.id.lbReason)
    HikLabelEditText lbReason;
    @BindView(R.id.lbMark)
    HikLabelEditText lbMark;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;

    EqpEventSpinnerAdapter eqpEventSpinnerAdapter;

    EqpStateInfo.XPdaEventsBean mXPdaEventsBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_state_modify;
    }

    @Override
    protected void initView() {
        EditTextEnterUtils.initPdaEditText(lbEqpID.getLabelContext(), true, text -> {
            loadEqpStateInfo(text);
        });
        tvSubmit.setOnClickListener(v -> {
            submit();
        });
        eqpEventSpinnerAdapter = new EqpEventSpinnerAdapter(null);
        lbSpinner.setAdapter(eqpEventSpinnerAdapter);
        lbSpinner.setOnItemSelectedListener((view, position) -> {
            EqpStateInfo.XPdaEventsBean eventsBean = eqpEventSpinnerAdapter.getItem(position);
            mXPdaEventsBean = eventsBean;
            loadEvent(lbEqpID.getLabelContext().getText().toString(), eventsBean);
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备状态修改");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void loadEqpStateInfo(String eqpId) {
        JsonObject map = new JsonObject();
//        map.addProperty("actionType", "logEvent");
        map.addProperty("eqpId", eqpId);
        NetUtils.requestDara("HIK.PdaLogEvent", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpStateInfo>>(new TypeToken<BaseResponseBean<EqpStateInfo>>() {}.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpStateInfo>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    setData(response.body().getBody());
                }
            }
        });
    }

    private void loadEvent(String eqpId, EqpStateInfo.XPdaEventsBean bean) {
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "getTargetState");
        map.addProperty("eqpId", eqpId);
        map.add("event", new Gson().toJsonTree(bean));
        NetUtils.requestDara("HIK.PdaLogEvent", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpStateInfo>>(new TypeToken<BaseResponseBean<EqpStateInfo>>() {}.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpStateInfo>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    setTargetState(response.body().getBody().getTargetState());
                }
            }
        });
    }

    private void submit() {
        if (TextUtils.isEmpty(lbEqpID.getLabelContext().getText())) {
            FreeApi_ToastUtils.showFreeToast("请录入设备ID", this, false, Toast.LENGTH_SHORT);
            return;
        }
        if (mXPdaEventsBean == null) {
            FreeApi_ToastUtils.showFreeToast("请选择事件", this, false, Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(lbReason.getLabelContext().getText())) {
            FreeApi_ToastUtils.showFreeToast("请输入动作原因", this, false, Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(lbDesc.getLabelContext().getText())) {
            FreeApi_ToastUtils.showFreeToast("请输入备注", this, false, Toast.LENGTH_SHORT);
            return;
        }
        String eqpID = lbEqpID.getLabelContext().getText().toString();
        String comment = lbMark.getLabelContext().getText().toString();
        String actionReason = lbReason.getLabelContext().getText().toString();

        logEvent(eqpID, mXPdaEventsBean, comment, actionReason);
    }

    private void logEvent(String eqpId, EqpStateInfo.XPdaEventsBean bean, String comment, String actionReason) {
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "logEvent");
        map.addProperty("eqpId", eqpId);
        map.add("event", new Gson().toJsonTree(bean));
        map.addProperty("comment", comment);
        map.addProperty("actionReason", actionReason);
        NetUtils.requestDara("HIK.PdaLogEvent", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpStateInfo>>(new TypeToken<BaseResponseBean<EqpStateInfo>>() {}.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpStateInfo>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showConfirmDialog(Eqp_StateModifyActivity.this, "修改成功");
                }
            }
        });
    }

    private void setData(EqpStateInfo data) {
        lbDesc.getLabelContext().setText(data.getDescription());
        lbState_category.getLabelContext().setText(data.getComClass());
        lbState.getLabelContext().setText(data.getState());
        lbState_sub.getLabelContext().setText(data.getSubState());
        eqpEventSpinnerAdapter.setData(data.getXPdaEvents());
    }

    private void setTargetState(String state) {
        lbState_target.getLabelContext().setText(state);
    }
}
